package com.example.ucs_alg_node_manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ucs_alg_node_manager.domain.Alg;
import com.example.ucs_alg_node_manager.domain.Node;
import com.example.ucs_alg_node_manager.domain.Task;
import com.example.ucs_alg_node_manager.mapper.NodeMapper;
import com.example.ucs_alg_node_manager.service.AlgService;
import com.example.ucs_alg_node_manager.mapper.AlgMapper;
import com.example.ucs_alg_node_manager.service.NodeService;
import com.example.ucs_alg_node_manager.service.TaskService;
import com.example.ucs_alg_node_manager.utils.RestTemplateUtil;
import com.example.ucs_alg_node_manager.utils.UcsPatterns;
import com.example.ucs_alg_node_manager.vo.ModelVO;
import com.example.ucs_alg_node_manager.vo.ReturnVO;
import com.example.ucs_alg_node_manager.vo.SystemStatus;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* @author 31586
* @description 针对表【ucs_alg】的数据库操作Service实现
* @createDate 2024-08-29 16:05:15
*/
@Service
public class AlgServiceImpl extends ServiceImpl<AlgMapper, Alg>
    implements AlgService{

    @Resource
    private RestTemplateUtil restTemplateUtil;

    @Resource
    private TaskService taskService;

    @Override
    public ReturnVO updateAlg(Alg alg) {
        String name = alg.getName();
        Alg byId = getById(alg.getId());
        if(!name.equals(byId.getName())){
            Alg one = getOne(Wrappers.<Alg>lambdaQuery().eq(Alg::getName, name));
            if (Optional.ofNullable(one).isPresent()) {
                return ReturnVO.fail().message("算法实例已经存在");
            }
        }
        updateById(alg);
        return ReturnVO.ok().message("算法信息配置成功");
    }

    @Override
    public ReturnVO uploadModel(MultipartFile file, Integer algId) {

        String filename = file.getOriginalFilename();
        // 获取文件后缀名，校验文件格式
        if(!filename.contains(".pth")){
            return ReturnVO.fail().message("文件格式有误");
        }
        Alg alg = getById(algId);
        if (!Optional.ofNullable(alg).isPresent()) {
            return ReturnVO.fail().message("算法信息不存在");
        }

        String message = "文件上传失败";
        // 上传文件
        try {
            SystemStatus<String> result = restTemplateUtil.uploadFile(file);
            String code = result.getCode();

            ObjectMapper objectMapper  = new ObjectMapper();

            if("err".equals(code)){
                // 文件上传失败
                message = objectMapper.readValue(result.getMsg(), String.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ReturnVO.fail().message(message);
        }
        alg.setModel(file.getOriginalFilename());
        updateById(alg);

        return ReturnVO.ok().message("模型上传成功");
    }

    @Override
    public ReturnVO selectModel(String model, Integer algId) {
        Alg alg = getById(algId);
        List<Task> list = taskService.list(Wrappers.<Task>lambdaQuery().eq(Task::getNodeId, alg.getNodeId()));
        for (Task task : list) {
            if("running".equals(task.getStatus())){
                return ReturnVO.fail().message("当前存在任务正在运行");
            }
        }
        alg.setModel(model);
        updateById(alg);
        return ReturnVO.ok().message("模型选择成功");
    }

    @Override
    public ReturnVO getModelList() {
        SystemStatus<List<ModelVO>> result = restTemplateUtil.getWithoutParams(UcsPatterns.GET_MODELS_URL);
        return ReturnVO.ok(result.getMsg()).message("模型列表获取成功");

    }

    @Override
    public ReturnVO delModel(String model) {

        return null;
    }
}




